package com.htt.app.amnhac12h.jsonparsing;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.htt.app.amnhac12h.config.Config;
import com.htt.app.amnhac12h.object.Show;

public class JSONParser {
	
	/**
	 * function return list show from server
	 */
public List<Show> getListShow(JSONArray jsonArray) {
		
		if(jsonArray == null)
			return null;
		
		List<Show> list = new ArrayList<Show>();
		
		try {
		    // looping through All show
		    for(int i = 0; i < jsonArray.length(); i++) {
		        JSONObject c = jsonArray.getJSONObject(i);
		        
		        // creata new Show
		        Show show = new Show();
		        
		        // adding values for Show
		        if(c.has(Config.TAG_Id))
		        	show.setId(c.getInt(Config.TAG_Id));
		        if(c.has(Config.TAG_Name))
		        	show.setName(c.getString(Config.TAG_Name));
		        if(c.has(Config.TAG_Desc))
		        	show.setDesc(c.getString(Config.TAG_Desc));
		        if(c.has(Config.TAG_Icon))
		        	show.setIcon(c.getString(Config.TAG_Icon));
		        if(c.has(Config.TAG_Source))
		        	show.setSource(c.getString(Config.TAG_Source));
		        if(c.has(Config.TAG_Order))
		        	show.setOrder(c.getInt(Config.TAG_Order));
		        
		        // adding Show to List
		        list.add(show);
		    }
		    
		} catch (JSONException e) {
		    e.printStackTrace();
		}
		
		return list;
	}
	
}
